package com.itheima.tlias.controller;

import com.itheima.aliyun.AliyunOSSUtils;
import com.itheima.aliyun.AliyunOssConfig;
import com.itheima.qiniuyun.QiniuyunKodoUtils;
import com.itheima.tlias.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


/**
 * 文件上传
 */
@Slf4j
@RestController

public class UploadController {
    //AliyunOssConfig获取
/*    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;*/


  //阿里云
    @Autowired
    private AliyunOssConfig aliyunOssConfig;
    @Autowired
    private AliyunOSSUtils aliyunOSSUtils;
  @PostMapping("/upload")
    public Result<String> upload(@RequestParam("file") MultipartFile file) throws Exception {
        //获取文件后缀名
        String originalFilename = file.getOriginalFilename();
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //获取文件的内容
        byte[] content = file.getBytes();
        String url = aliyunOSSUtils.upload(/*aliyunOssConfig.getEndpoint(),aliyunOssConfig.getBucketName(),*/content, extName);
        return Result.success(url);

    }

   /* //七牛云
    @Autowired
    private QiniuyunKodoUtils qiniuyunKodoUtilis;
   // @MyComment(desc = "文件上传")
    @PostMapping("/upload")
    public Result<String> upload(@RequestParam("file") MultipartFile file) throws Exception {
        //获取文件后缀名
        String originalFilename = file.getOriginalFilename();
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //获取文件的内容
        byte[] content = file.getBytes();
        String url = qiniuyunKodoUtilis.upload( content, extName);
        return Result.success(url);

    }
*/



/*  JDK  API

  @PostMapping("/upload")
    public String upload(String username,
                         Integer age,
                         @RequestParam("file") MultipartFile file) throws Exception {
        // 获取上传的文件的文件名
        String originalFilename = file.getOriginalFilename();
        //获取的表单项的名字
        String name = file.getName();
        log.info("username:{}, age:{}, originalFilename:{}", username, age, originalFilename);
        // 获取上传的文件的输入流
        //InputStream inputStream = file.getInputStream();
        String substring = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        UUID uuid = UUID.randomUUID();
        File dst = new File("d:/", uuid + substring);
        file.transferTo(dst);
        // 返回url地址
        //return "success";
        return "http://localhost:8080/download?filename=" + originalFilename;
    }

    @GetMapping("/download")
    public void download(String filename, HttpServletResponse response) throws Exception {
        // 设置响应数据的类型
        response.setContentType("image/png");
        // 构造要下载的文件
        File dst = new File("d:/", filename);
        // 获取文件的输入流
        FileInputStream fileInputStream = new FileInputStream(dst);
        // 获取响应的输出流
        ServletOutputStream outputStream = response.getOutputStream();
        IOUtils.copyLarge(fileInputStream, outputStream);
        // 关闭资源
        fileInputStream.close();
        outputStream.close();
    }*/
}